package com.iecg.controller.websocket;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.iecg.api.entity.IecgCheckData;
import com.iecg.api.entity.IecgEquip;
import com.iecg.service.IecgCheckDataService;
import com.iecg.service.IecgEquipService;
import com.pig4cloud.pigx.common.core.util.R;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author niuyy
 * @date 2018/3/23
 */
@RestController
@AllArgsConstructor
@Slf4j
public class WebController {

	private final SimpMessagingTemplate messagingTemplate;

	private final IecgEquipService iecgEquipService;

	private final IecgCheckDataService iecgCheckDataService;

	private final RedisTemplate redisTemplate;


	/**
	 * 接收消息
	 * @param name 姓名
	 * @return welcome, [姓名] !
	 */
	@MessageMapping("/welcome")
	@SendTo("/topic/getBro")
	public String say(String name) {
		log.info("name: " + name);
		return "welcome, " + name + " !";
	}



	/**
	 * 广播式发送消息给订阅了「/topic/getBro」的客户端
	 */
	@RequestMapping("sendMsgBro")
	@ResponseBody
	public void sendMsg() {
		messagingTemplate.convertAndSend("/topic/getBro", "服武器主动推送的广播消息");
	}

	/**
	 * 发送消息给指定 sessionId 的客户端, 且该客户端订阅了「/topic/getBro」
	 *
	 * @param sessionId 客户端的 sessionId
	 */
	@RequestMapping("sendMsgPoint")
	@ResponseBody
	public void sendMsgPoint(String sessionId) {
		messagingTemplate.convertAndSendToUser(sessionId, "/queue/getPoint", "服武器主动推送的点对点消息", createHeaders(sessionId));
	}

	private MessageHeaders createHeaders(String sessionId) {
		SimpMessageHeaderAccessor headerAccessor = SimpMessageHeaderAccessor.create(SimpMessageType.MESSAGE);
		headerAccessor.setSessionId(sessionId);
		headerAccessor.setLeaveMutable(true);
		return headerAccessor.getMessageHeaders();
	}



	/**
	 * 接收消息
	 * @param name 姓名
	 * @return welcome, [姓名] !
	 */
	@MessageMapping("/equip/list")
	@SendTo("/topic/equip/list")
	public Object equipList(String name) {
		log.info("name: " + name);
		List<IecgEquip> equipList = iecgEquipService.list(Wrappers.<IecgEquip>query()
				.lambda().eq(IecgEquip::getUserId, "-1"));
		return equipList;
	}



	@MessageMapping("/equip/update")
	@SendTo("/topic/equip/result")
	@SneakyThrows
	public Object equipUpdate(String name) {
		log.info("name: " + name);
		IecgEquip iecgEquip = JSONUtil.toBean(name, IecgEquip.class);
		iecgEquip.setDelFlag("0");
		iecgEquip.setUserId(-1L);
		Boolean isupate = iecgEquipService.update(iecgEquip, Wrappers.<IecgEquip>update().lambda().eq(IecgEquip::getPdid, iecgEquip.getPdid()));
		if(!isupate){

			iecgEquipService.save(iecgEquip);
		}
		return R.ok();
	}


	@MessageMapping("/equip/remove")
	@SendTo("/topic/equip/result")
	public Object equipRemove(String name) {
		iecgEquipService.remove(Wrappers.<IecgEquip>query()
				.lambda().eq(IecgEquip::getPdid, name));
		return R.ok();
	}


	@MessageMapping("/checkdata/save")
	@SendTo("/topic/checkdata/result")
	@SneakyThrows
	public Object checkdataSave(String name) {
		log.info("name: " + name);
		IecgCheckData checkData = JSONUtil.toBean(name, IecgCheckData.class);
		iecgCheckDataService.save(checkData);

		return R.ok(checkData);
	}
}